package com.dao.Impl;

import com.dao.goodDAO;
import com.entity.goods;
import com.DBUtil.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
    
import java.sql.ResultSet;
import java.sql.PreparedStatement;

public class goodDAOImpl implements goodDAO {
    @Override
    public List<goods> ListGoods() {
        List<goods> list = new ArrayList<>();
        String sql = "select * from goods";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            ResultSet rst = pst.executeQuery();
            while (rst.next()){
                String id = rst.getString("good_id");
                String name = rst.getString("good_name");
                double price = rst.getDouble("good_price");
                String date = rst.getString("good_date");
                int xiajia = rst.getInt("good_status");
                goods good = new goods(id,name,price,date,xiajia);
                list.add(good);
            }
            DBUtil.close(rst,pst,conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public goods getGoodById(String id) {
        goods good = null;
        String sql = "select * from goods where good_id = ?";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,id);
            ResultSet rst = pst.executeQuery();
            if(rst.next()){
                String name = rst.getString("good_name");
                int price = rst.getInt("good_price");
                String date = rst.getString("good_date");
                int xiajia = rst.getInt("good_status");
                good = new goods(id,name,price,date,xiajia);
            }
            DBUtil.close(rst,pst,conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return good;
    }

    @Override
    public int addGood(goods good) {
        int count = 0;
        String sql = "insert into goods (good_id,good_name,good_price,good_date,good_status) values (?,?,?,?,?)";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,good.getGood_id());
            pst.setString(2,good.getGood_name());
            pst.setDouble(3,good.getGood_price());
            pst.setString(4,good.getGood_date());
            pst.setInt(5,good.getGood_status());
            count = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public int delGood(String id) {
        String sql = "delete from goods where good_id = ?";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        int count = 0;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,id);
            count = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public int updateGood(goods good) {
        int count = 0;
        String sql = "update goods set good_name = ?,good_price = ?,good_date = ?,good_status = ? where good_id = ?";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,good.getGood_name());
            pst.setDouble(2,good.getGood_price());
            pst.setString(3,good.getGood_date());
            pst.setInt(4,good.getGood_status());
            pst.setString(5,good.getGood_id());
            count = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public int delAllGood(ArrayList<String> ids) {
        int count = 0;
        String sql = "delete from goods where good_id in (";
        for(int i = 0; i < ids.size(); i++){
            sql += "?";
            if(i != ids.size() - 1){
                sql += ",";
            }
        }
        sql += ")";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            for(int i = 0; i < ids.size(); i++){
                pst.setString(i+1,ids.get(i));
            }
            count = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }
    public static void main(String[] args) {
        System.out.println(new goodDAOImpl().ListGoods());
    }
}
